package bdqn.com.cui.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
   static String driver=null;
    static String url=null;
    static String user=null;
    static String password=null;

    static {
     InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
     Properties properties=new Properties();
     try {
      properties.load(is);
      driver=properties.getProperty("driver");
      url=properties.getProperty("url");
      user=properties.getProperty("user");
      password=properties.getProperty("password");
      try {
       Class.forName(driver);
      } catch (ClassNotFoundException e) {
       throw new RuntimeException(e);
      }
     } catch (IOException e) {
      throw new RuntimeException(e);
     }
    }



    public Connection getconn(){
     Connection conn=null;
  try {
  conn= DriverManager.getConnection(url,user,password);
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
  return conn;
 }

    public void getclass(ResultSet resul, PreparedStatement ps,Connection conn){
           if (resul!=null){
            resul.getClass();
           }

     if (ps!=null){
      ps.getClass();
     }

     if (conn!=null){
      conn.getClass();
     }
    }


    public int Crud(String sql,Object... obj){
    Connection conn= getconn();
    PreparedStatement ps=null;
     try {
      ps=conn.prepareStatement(sql);
      for (int i = 0; i < obj.length; i++) {
       if (obj[i]!=null){
        ps.setObject(1+i,obj[i]);
       }
      }
         return ps.executeUpdate();
     } catch (SQLException e) {
      throw new RuntimeException(e);
     }finally {
      getclass(null,ps,conn);
     }

    }


}



